/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package appClientHandler;

import entities.Account;
import entities.Cart;
import entities.Commande;
import entities.Product;
import sessions.AccountFacadeLocal;
import sessions.ProductFacadeLocal;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;


/**
 *
 * @author William
 */
public class AUPBeanGestionCart implements AUPBeanGestionCartLocal 
{
    // Todo : ajouter la gestion des commandes via les beans Remote.
    @EJB
    AccountFacadeLocal accountFacade;

    @EJB
    ProductFacadeLocal productFacade;

     public List<Commande> getCartContents(Cart cart) 
     {
	List<Commande> result = new ArrayList<Commande>();

	return result;
    }



    public void addProductToCart(Cart cart, int idProduct) 
    {
	Product product = this.productFacade.find(idProduct);
	if (product != null) 
        {
	    // TODO ( W. ) : Ajouter le Product au panier
	}
    }

    public void removeProductFromCart(Cart cart, int idProduct) 
    {
	Product product = this.productFacade.find(idProduct);
	if (product != null) {
	    // TODO ( W. ) : Retirer le Product du panier
	}
    }

    public void buyCartWith(Cart cart, String iban) 
    {
	try 
        {
            // TODO ( Tuan ) : Procéder au paiement

	} 
        catch (ConstraintViolationException cv) 
        {
	    System.out.println("AUPBeanGestionCart - CONSTRAINT VIOLATION EXCEPTION : " + cv.getMessage());
	    Iterator it = cv.getConstraintViolations().iterator();
	    while (it.hasNext()) 
            {
		ConstraintViolation c = (ConstraintViolation) it.next();
		System.out.println("AUPBeanGestionCart - CONSTRAINT VIOLATION : " + c);
            }


	} 
        catch (Exception e) 
        {
	    e.printStackTrace(System.err);

	}
    }

    @Override
    public Long getTotalValue(Cart cart) {
	Long totalVue = 0l;
        /*
	Map<Integer, Integer> items = cart.getItems();
	if (items != null && !items.isEmpty()) {

	    for (Entry<Integer, Integer> entry : items.entrySet()) {
		Product p = this.productFacade.find(entry.getKey());
		totalVue += p.getPrix() * entry.getValue();
	    }
	}
        * */
	return totalVue;
    }
    
}
